package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.pay.client.entity.PayConfig;
import com.meida.module.pay.client.entity.PayInfo;
import com.meida.module.system.client.entity.SysDictData;
import com.meida.module.user.client.entity.AppEducational;
import com.meida.module.user.client.entity.AppSchool;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 后台支付流水列表
 */
@Component
public class PayInfoPageListHandler implements PageInterceptor<PayInfo> {


    @Override
    public void prepare(CriteriaQuery<PayInfo> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        String userContent = params.get("userContent");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        cq.select(PayInfo.class, "payInfoId", "busId", "orderEntity", "orderTitle", "orderBody", "orderImage", "outTradeNo", "orderAmount", "payStatus", "payType", "payDate", "returnDate", "returnAmount", "reason", "createTime");
        cq.select(AppUser.class, "userName", "nickName", "mobile", "userNo");
        cq.eq(AppEducational.class, "userId");
        if (FlymeUtils.isNotEmpty(userContent)) {
            cq.and(true, q -> {
                q.like("user.nickName", userContent);
                q.or().like("user.userName", userContent);
                q.or().like("user.mobile", userContent);
                q.or().like("user.userNo", userContent);
            });
        }
        cq.eq(BusOrder.class, "orderType");
        cq.eq(PayInfo.class, "payStatus");
        cq.ge("DATE_FORMAT(payinfo.createTime,'%Y-%m-%d')", beginDate);
        cq.le("DATE_FORMAT(payinfo.createTime,'%Y-%m-%d')", endDate);
        cq.createJoin(AppUser.class).setMainField("userId").setJoinField("userId");
        cq.createJoin(BusOrder.class).setMainField("busId").setJoinField("orderId");
    }

    @Override
    public void complete(CriteriaQuery<PayInfo> cq, List<EntityMap> result, EntityMap extra) {
        result.forEach(entityMap -> {
            String orderEntity = entityMap.get("orderEntity");
            if (orderEntity.equals(BusOrder.class.getSimpleName())) {
                Long busId = entityMap.getLong("busId");
                entityMap.put("orderId", busId);
            }
        });
    }


}
